//
//  DiscoverClassifyCollectionViewCellFifth.m
//  XYFMApp
//
//  Created by 岁变 on 16/9/7.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "DiscoverClassifyCollectionViewCellFifth.h"
#import "XYMacro.h"

@interface DiscoverClassifyCollectionViewCellFifth () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *mArr;
@property (nonatomic, assign) NSInteger count;

@end

@implementation DiscoverClassifyCollectionViewCellFifth

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        _mArr = [NSMutableArray array];
        _count = 1;
        _tableView = [[BaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [self.contentView addSubview:_tableView];
        [_tableView registerClass:[DiscoverClassifyTableViewCellThird class] forCellReuseIdentifier:@"DiscoverClassifyTableViewCellThird"];
        [self pullDownRefresh];
        
    }
    return self;
}

#pragma mark --- 刷新---------------

- (void)pullDownRefresh {
    MJRefreshGifHeader *header = [MJRefreshGifHeader  headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [self.tableView.mj_header beginRefreshing];
    
    NSMutableArray *mArr = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"list_load_%d", i]];
        [mArr addObject:image];
    }
    
    [header setImages:mArr forState:MJRefreshStateRefreshing];
    header.stateLabel.hidden = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = header;
}

- (void)loadNewData {
    if (_model.tag == 1) {
        NSString *str1 = @"json=%7B%22cat_id%22%3A%22";
        NSString *str2 = @"%22%2C%22orderby%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%2C%22searchtype%22%3A0%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22keyword%22%3A%22%22%2C%22target_id%22%3A%220%22%7D";
        NSString *body = [[str1 stringByAppendingString:_cat_id] stringByAppendingString:str2];
        [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:body andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
            ClassifyModelSecond *model = [ClassifyModelSecond modelWithDictionary:result];
            if (model.status.succeed == 1) {
                //[_mArr removeAllObjects];
                _mArr = (NSMutableArray *)model.data;
                [self.tableView reloadData];
                [self.tableView.mj_header endRefreshing];
            }
            
        } andFailureBlock:^(NSError *error) {}];

    }
    if (_model.tag == 2) {
        
        NSString *str1 = @"json=%7B%22cat_id%22%3A%22";
        NSString *str2 = @"%22%2C%22orderby%22%3A1%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%2C%22searchtype%22%3A0%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22keyword%22%3A%22%22%2C%22target_id%22%3A%220%22%7D";
        NSString *body = [[str1 stringByAppendingString:_cat_id] stringByAppendingString:str2];
        [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:body andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
            ClassifyModelSecond *model = [ClassifyModelSecond modelWithDictionary:result];
            if (model.status.succeed == 1) {
                //[_mArr removeAllObjects];
                _mArr = (NSMutableArray *)model.data;
                [self.tableView reloadData];
                [self.tableView.mj_header endRefreshing];
            }

        } andFailureBlock:^(NSError *error) {}];
    }
}


#pragma mark -------- 加载----------
- (void)setupUpRefresh {
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreBills)];
}


- (void)loadMoreBills {
    _count += 1;
    if (_model.tag == 1) {
        NSString *str1 = @"json=%7B%22cat_id%22%3A%22";
        NSString *str2 = @"%22%2C%22orderby%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A";
        NSString *str3 = @"%7D%2C%22searchtype%22%3A0%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22keyword%22%3A%22%22%2C%22target_id%22%3A%220%22%7D";
        NSString *body = [[[[str1 stringByAppendingString:_cat_id] stringByAppendingString:str2] stringByAppendingString:[NSString stringWithFormat:@"%ld", _count]] stringByAppendingString:str3];
        [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:body andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
            ClassifyModelSecond *model = [ClassifyModelSecond modelWithDictionary:result];
            if (model.paginated.more == 1) {
                [_mArr addObjectsFromArray:model.data];
                [self.tableView reloadData];
                [self.tableView.mj_footer endRefreshing];
            } else {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }

            
        } andFailureBlock:^(NSError *error) {}];
    }
    if (_model.tag == 2) {
        
        NSString *str1 = @"json=%7B%22cat_id%22%3A%22";
        NSString *str2 = @"%22%2C%22orderby%22%3A1%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A";
        NSString *str3 = @"%7D%2C%22searchtype%22%3A0%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22keyword%22%3A%22%22%2C%22target_id%22%3A%220%22%7D";
        NSString *body = [[[[str1 stringByAppendingString:_cat_id] stringByAppendingString:str2] stringByAppendingString:[NSString stringWithFormat:@"%ld", _count]] stringByAppendingString:str3];
        [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:body andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
            ClassifyModelSecond *model = [ClassifyModelSecond modelWithDictionary:result];
            if (model.paginated.more == 1) {
                [_mArr addObjectsFromArray:model.data];
                [self.tableView reloadData];
                [self.tableView.mj_footer endRefreshing];
            } else {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
        } andFailureBlock:^(NSError *error) {}];
    }
    
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _mArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DiscoverClassifyTableViewCellThird *cell = [tableView dequeueReusableCellWithIdentifier:@"DiscoverClassifyTableViewCellThird" forIndexPath:indexPath];
    cell.model = [_mArr objectAtIndex:indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    VCOfClassify *classifyVC = (VCOfClassify *)self.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder;
    VCOfDiscoverList *nextVC = [[VCOfDiscoverList alloc] init];
    ClassifyModelSecondData *model = [_model.data objectAtIndex:indexPath.row];
    nextVC.item_id = [NSString stringWithFormat:@"%ld", model.item_id];
    [classifyVC.navigationController pushViewController:nextVC animated:YES];
}

- (void)setModel:(ClassifyModelSecond *)model {
    _model = [[ClassifyModelSecond alloc] init];
    _model = model;
    _mArr = (NSMutableArray *)model.data;
    [self.tableView reloadData];
    if (model.paginated.more == 1) {
        [self setupUpRefresh];
    }
    

}



- (void)layoutSubviews {
    [super layoutSubviews];
    _tableView.frame = self.contentView.bounds;
}




@end
